package com.sais.manage.boot.repair.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sais.manage.boot.building.dto.RoomDTO;
import com.sais.manage.boot.building.mapper.RoomMapper;
import com.sais.common.utils.UUIDUtils;
import com.sais.manage.boot.repair.dto.RepairDTO;
import com.sais.manage.boot.repair.dto.RepairSaveDTO;
import com.sais.manage.boot.repair.dto.RepairSearchDTO;
import com.sais.manage.boot.repair.entity.Repair;
import com.sais.manage.boot.repair.mapper.RepairMapper;
import com.sais.manage.boot.repair.service.IRepairService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 宿舍管理系统-维修申请 service 实现类
 *
 * @author zwp
 * @date 2020/2/2
 **/
@Service
@Slf4j
public class RepairServiceImpl implements IRepairService {


    @Resource
    private RepairMapper repairMapper;

    @Resource
    private RoomMapper roomMapper;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean saveRepair(RepairSaveDTO repairSaveDTO) {
        String ukId = repairSaveDTO.getUkId();
        if (StringUtils.isEmpty(ukId)) {
            createRepair(repairSaveDTO);
        } else {
            updateRepair(repairSaveDTO);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean createRepair(RepairSaveDTO repairSaveDTO) {
        Repair repair = repairSaveDTO.convert();
        repair.setUkId(UUIDUtils.getLower());

        List<String> propertyIds = repairSaveDTO.getPropertyIds();
        if (!CollectionUtils.isEmpty(propertyIds)) {
            bindRepairProperty(repair.getUkId(), propertyIds);
        }

        //设置 维修单号为 房间编码 + 日期 + 当前宿舍楼当天报修个数
        String roomId = repair.getRoomId();
        LocalDateTime comingTime = repair.getComingTime();
        String comingTimeStr = String.valueOf(comingTime.getYear()) + comingTime.getMonthValue() + comingTime.getDayOfMonth();
        int count = repairMapper.selectCountByRoomId(roomId, LocalDateTime.MIN, LocalDateTime.MAX) + 1;

        RoomDTO room = roomMapper.selectRoom(roomId);
        String code = room.getCode()+comingTimeStr+count;
        repair.setCode(code);

        repairMapper.createRepair(repair);
        return true;
    }


    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean updateRepair(RepairSaveDTO repairSaveDTO) {
        Repair repair = repairSaveDTO.convert();
        repair.setGmtModified(LocalDateTime.now());
        repairMapper.updateById(repairSaveDTO.convert());
        return true;

    }


    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean deleteRepair(String repairSaveDTO) {
        repairMapper.deleteRepair(repairSaveDTO);
        return true;
    }


    @Override
    public RepairDTO selectRepair(String repairId) {
        RepairDTO repairDTO = repairMapper.selectRepair(repairId);
        return repairDTO;
    }


    @Override
    public Page<RepairDTO> listRepair(Integer current, Integer size, RepairSearchDTO repairSearchDTO) {
        Page<RepairDTO> page = new Page<>(current, size);
        return repairMapper.listRepair(page, repairSearchDTO);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean bindRepairProperty(String repairId, List<String> propertyIds) {
        repairMapper.bindRepairProperty(repairId, propertyIds);
        return true;
    }

    @Override
    public boolean updateRepairStatus(String repairId) {
        Repair repair = Repair.builder()
                .ukId(repairId)
                .status(1)
                .build();
        repairMapper.updateById(repair);
        return true;
    }
}
